<?php

@include_once('./database.php');



// 首先接受前端的数据
$uphonenumber = $_POST['phonenumber'];
$uname = $_POST['username'];
$upwd = $_POST['password'];


// 给前端返回的结果
$obj = [];


// 判断是否接收到
if ($uphonenumber && $uname && $upwd) {
    // 判断用手机号是否存在
    // 先定义sql语句
    $sql = "select * from user where user_phonenumber = '$uphonenumber'";
    // 运行sql语句   mysqli_query()
    $res = mysqli_query($connect, $sql);
    // 把表格转成数组   mysqli_fetch_array()
    $arr = mysqli_fetch_array($res);   // $arr = ['username' => 'yy' , 'password' => '123']

    // 判断结果是否存在
    if ($arr) {
        $obj['status'] = false;
        $obj['msg'] = '手机号已经被注册';
    } else {
        // 注册 -- 把数据插入到数据库
        // 定义sql语句
        $sql = "insert into user (user_phonenumber , user_name , user_password) values ('$uphonenumber' , '$uname' , '$upwd')";
        // 运行
        mysqli_query($connect, $sql);
        // 找到受影响的行数
        $rows = mysqli_affected_rows($connect);
        if ($rows > 0) {
            $obj['status'] = true;
            $obj['msg'] = '注册成功';
        } else {
            $obj['status'] = false;
            $obj['msg'] = '服务器错误';
        }
    }
} else {
    $obj['status'] = false;
    $obj['msg'] = '前端数据有误';
}


// 返回给前端的数据  --- json格式的字符串
echo (json_encode($obj, JSON_UNESCAPED_UNICODE));
